/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProjectTaskReport.java
 *
 * Created on Jul 20, 2011, 10:40:36 PM
 */

package javadatabasegui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author PM
 */
public class ProjectTaskReport extends javax.swing.JFrame {

    Connection con;
    int projectID;
    String userEmail;
    String projectName;
    /** Creates new form ProjectTaskReport */
    public ProjectTaskReport(int pid, String uemail, String pn, Connection conn) {
        con = conn;
        projectID = pid;
        userEmail = uemail;
        projectName = pn;        
        initComponents();
        displayInfo();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("GT Project Manager - Project Tasks Report");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(230, 174, 39));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setText("Project Tasks Report for ");

        jLabel2.setFont(new java.awt.Font("Tahoma", 3, 24));
        jLabel2.setText(projectName);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel3.setText("Task Name");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel4.setText("Due Date");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel5.setText("Assigned To");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel6.setText("Total Hours");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel7.setText("Status");

        jSeparator1.setBackground(new java.awt.Color(0, 0, 0));

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
        jScrollPane1.setBorder(null);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setPreferredSize(new java.awt.Dimension(875, 413));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 903, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 414, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jPanel3);

        jSeparator2.setPreferredSize(new java.awt.Dimension(903, 10));

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 16));
        jLabel8.setText("Project Total Hours");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 16));
        jLabel9.setText("jLabel9");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 262, Short.MAX_VALUE)
                            .addComponent(jLabel4)
                            .addGap(58, 58, 58)
                            .addComponent(jLabel5)
                            .addGap(145, 145, 145)
                            .addComponent(jLabel7)
                            .addGap(42, 42, 42)
                            .addComponent(jLabel6)
                            .addGap(57, 57, 57))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 903, Short.MAX_VALUE)
                                .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 903, Short.MAX_VALUE))
                            .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 903, Short.MAX_VALUE)
                            .addContainerGap()))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(39, 39, 39)
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(37, 37, 37))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 3, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 414, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 610, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        new UserDashboard(userEmail, con).setVisible(true);
                this.dispose();
    }//GEN-LAST:event_formWindowClosing

    private void displayInfo(){

        int numTasks = 0;
        int numMile = 0;
        float totalHours = 0;

        //Gets the number of entries in the box, needed to set the size of the box (for scrolling).
        try{
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            if (stmt.execute("SELECT COUNT(DISTINCT Milestones.M_Name) as mName, COUNT(DISTINCT T_NAME, Tasks.T_Name) as tName "
                    + "FROM `cs4400_Group_14`.Tasks, `cs4400_Group_14`.Milestones "
                    + "WHERE Milestones.PID = "+ projectID + " AND Tasks.PID = " + projectID)){
                rs = stmt.getResultSet();
            }
            rs.next();
            numTasks = rs.getInt("tName");
            numMile = rs.getInt("mName");
        }catch(SQLException e){
            System.err.println("Error counting milestones and tasks");
            System.err.println(e.getMessage());
            }

        //Sets the size of the panel to the correct height for the number of entries.
        jPanel3.setPreferredSize(new java.awt.Dimension(890, (numTasks+numMile)*25));
        
        //Creats an array type to hold the labels (labels are each line in the panel)
        javax.swing.JLabel[] mlsLabels = new javax.swing.JLabel[numMile];
        javax.swing.JLabel[] tskLabels = new javax.swing.JLabel[numTasks];
        for(int i=0; i<mlsLabels.length; i++){ mlsLabels[i] = new javax.swing.JLabel(); }
        for(int i=0; i<tskLabels.length; i++){ tskLabels[i] = new javax.swing.JLabel(); }
        
        try{
            Statement stmt = con.createStatement();
            Statement stmt1 = con.createStatement();
            ResultSet rs = null;
            ResultSet rs1 = null;
            
            //This query retrieves the milestone tuples
            if (stmt.execute("SELECT M_Name, Milestones.Due_Date as MS_Due_Date,"
                    + " Milestones.`Status` as MS_Status, (Select SUM(Worklog.Hours)"
                    + " FROM `cs4400_Group_14`.Worklog WHERE"
                    + " Worklog.PID = "+ projectID +" AND Worklog.M_Name = Milestones.M_Name) as tHours"
                    + " FROM `cs4400_Group_14`.Milestones WHERE Milestones.PID = "+projectID
                    + " GROUP BY Milestones.Due_Date")){
                rs = stmt.getResultSet();
            }
            String str = "Complete";
            for(int i=0; rs.next();i++){
                if(rs.getInt("MS_Status") == 1){str = "Active";}
                mlsLabels[i].setText("<html> <body> <table width=\"890\"> <tr>"
                        + "<td width=\"37%\">" + rs.getString("M_Name") + "</td>"
                        + "<td width=\"40%\">" + rs.getDate("MS_Due_Date") + "</td>"
                        + "<td width=\"13%\">" + str + "</td>"
                        + "<td width=\"10%\">" + rs.getFloat("tHours") + "</td>"
                        + "</tr> </table> </body> </html>");
                mlsLabels[i].setName(rs.getString("M_Name"));
                str = "Complete";
                totalHours = totalHours + rs.getFloat("tHours");
            }
            
            //This query retrieves the tasks tuples
            if (stmt1.execute("SELECT T_Name, M_Name, Due_Date, F_Name, L_Name, `Status`, "
                    + "(SELECT SUM(Hours) FROM `cs4400_Group_14`.Worklog WHERE PID = "+ projectID
                    + " AND Worklog.T_Name = Tasks.T_Name AND Worklog.M_Name = Tasks.M_Name) as tHours "
                    + "FROM `cs4400_Group_14`.Tasks, `cs4400_Group_14`.Users "
                    + "WHERE Tasks.PID = "+projectID+" AND Tasks.U_Email = Users.Email "
                    + "GROUP BY Due_Date")){
                rs1 = stmt1.getResultSet();
            }
            String strp = "Closed";
            for(int i=0; rs1.next(); i++){
                if(rs1.getInt("Status") == 1){strp = "Open";}
                tskLabels[i].setText("<html> <body> <table width=\"890\"> <tr>"
                        + "<td width=\"37%\">" + rs1.getString("T_Name") + "</td>"
                        + "<td width=\"13%\">" + rs1.getDate("Due_Date") + "</td>"
                        + "<td width=\"27%\">" + rs1.getString("F_Name") + " "
                        + rs1.getString("L_Name") + "</td>"
                        + "<td width=\"13%\">" + strp + "</td>"
                        + "<td width=\"10%\">" + rs1.getFloat("tHours") + "</td>"
                        + "</tr> </table> </body> </html>");
                tskLabels[i].setName(rs1.getString("M_Name"));
                strp = "Closed";
            }      
        }catch(SQLException e){
            System.err.println("Error counting getting data for milestones and/or tasks");
            System.err.println(e.getMessage());
            System.err.println(e.getSQLState());
        }
            jPanel3.setLayout(new GridBagLayout());
            GridBagConstraints cnst = new GridBagConstraints();
            int position = 0;
            String st = null;
            String sr = null;
            for(int i=0; i<mlsLabels.length; i++){
                mlsLabels[i].setFont(new Font("Tahoma",Font.BOLD,16));
                mlsLabels[i].setBackground(new Color(129,211,252));
                mlsLabels[i].setOpaque(true);
                cnst.gridx = 0;
                cnst.gridy = position;
                cnst.ipady = 4;
                jPanel3.add(mlsLabels[i], cnst);
                position++;
                st = mlsLabels[i].getName();
                for( int j=0; j<tskLabels.length; j++){
                    sr = tskLabels[j].getName();
                    if( st.equals(sr) ){
                        tskLabels[j].setFont(new Font("Tahoma",Font.PLAIN,12));
                        cnst.gridx = 0;
                        cnst.gridy = position;
                        jPanel3.add(tskLabels[j], cnst);
                        position++;
                    }
                }
                cnst.gridx = 0;
                cnst.gridy = position;
                cnst.fill = GridBagConstraints.HORIZONTAL;
                javax.swing.JLabel sep = new javax.swing.JLabel();
                sep.setPreferredSize(new Dimension(1,2));
                sep.setBackground(Color.WHITE);
                sep.setOpaque(true);
                jPanel3.add(sep, cnst);
                position++;                
            }
            cnst.gridy = position;
            cnst.gridx = 0;
            cnst.weighty = (numTasks + numMile) * 25;
            jPanel3.add(new javax.swing.JLabel(), cnst);

            jLabel9.setText(Float.toString(totalHours));
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    // End of variables declaration//GEN-END:variables

}
